/**
 * 编辑工具箱事件
 */
class Event {
  constructor(_) {
    this.tool = _;
    this._drawCreateHandle = this.drawCreateHandle.bind(this);
    this._drawDeleteHandle = this.drawDeleteHandle.bind(this);
    this._drawUpdateHandle = this.drawUpdateHandle.bind(this);
    this._drawDragmoveHandle = this.drawDragmoveHandle.bind(this);
    this._drawSelectionchangeHandle = this.drawSelectionchangeHandle.bind(this);
    this._drawModechangeHandle = this.drawModechangeHandle.bind(this);
    this._drawActionableHandle = this.drawActionableHandle.bind(this);
    this._drawRenderHandle = this.drawRenderHandle.bind(this);
    this._drawCombineHandle = this.drawCombineHandle.bind(this);
    this._drawUncombineHandle = this.drawUncombineHandle.bind(this);
    this._drawDragVertexHandle = this.drawDragVertexHandle.bind(this);
    this._drawUnionHandle = this.drawUnionHandle.bind(this);
    this._keyDown = this.keyDown.bind(this);
  }

  /**
   * 绑定事件
   */
  addEvents() {
    this.tool.getMap().on('draw.create', this._drawCreateHandle);
    this.tool.getMap().on('draw.delete', this._drawDeleteHandle);
    this.tool.getMap().on('draw.update', this._drawUpdateHandle);
    this.tool.getMap().on('draw.dragmove', this._drawDragmoveHandle);
    this.tool.getMap().on('draw.selectionchange', this._drawSelectionchangeHandle);
    this.tool.getMap().on('draw.modechange', this._drawModechangeHandle);
    this.tool.getMap().on('draw.actionable', this._drawActionableHandle);
    this.tool.getMap().on('draw.render', this._drawRenderHandle);
    this.tool.getMap().on('draw.combine', this._drawCombineHandle);
    this.tool.getMap().on('draw.uncombine', this._drawUncombineHandle);
    this.tool.getMap().on('draw.dragVertex', this._drawDragVertexHandle);
    this.tool.getMap().on('draw.union', this._drawUnionHandle);
    this.tool.getMap().getContainer().addEventListener('keydown', this._keyDown);
  }

  /**
   * 移除事件
   */
  removeEvents() {
    this.tool.getMap().off('draw.create', this._drawCreateHandle);
    this.tool.getMap().off('draw.delete', this._drawDeleteHandle);
    this.tool.getMap().off('draw.update', this._drawUpdateHandle);
    this.tool.getMap().off('draw.dragmove', this._drawDragmoveHandle);
    this.tool.getMap().off('draw.selectionchange', this._drawSelectionchangeHandle);
    this.tool.getMap().off('draw.modechange', this._drawModechangeHandle);
    this.tool.getMap().off('draw.actionable', this._drawActionableHandle);
    this.tool.getMap().off('draw.render', this._drawRenderHandle);
    this.tool.getMap().off('draw.combine', this._drawCombineHandle);
    this.tool.getMap().off('draw.uncombine', this._drawUncombineHandle);
    this.tool.getMap().off('draw.dragVertex', this._drawDragVertexHandle);
    this.tool.getMap().off('draw.union', this._drawUnionHandle);
    this.tool.getMap().getContainer().removeEventListener('keydown', this._keyDown);
  }

  /**
   * 绘制新增事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawCreateHandle(e) {
    this.processCallback('create', e);
    if(this.tool.snapUtil) {
      this.tool.snapUtil.setSnapVertex([]);
    }
    if(this.tool.events.onCreate) {
      this.tool.events.onCreate(e);
    }
  }

  /**
   * 绘制删除事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawDeleteHandle(e) {
    this.processCallback('delete', e);
    if(this.tool.events.onDelete) {
      this.tool.events.onDelete(e);
    }
  }

  /**
   * 绘制更新事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawUpdateHandle(e) {
    this.processCallback('update', e);
    if(this.tool.events.onUpdate) {
      this.tool.events.onUpdate(e);
    }
  }

  /**
   * 绘制拖动事件
   * @param {*} e 事件参数
   */
  drawDragmoveHandle(e) {
    this.processCallback('dragmove', e);
    if(this.tool.events.onDragmove) {
      this.tool.events.onDragmove(e);
    }
  }

  /**
   * 绘制选中变更事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawSelectionchangeHandle(e) {
    this.processCallback('selectionchange', e);
    if(this.tool.events.onSelectionchange) {
      this.tool.events.onSelectionchange(e);
    }
  }

  /**
   * 绘制模式变更事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawModechangeHandle(e) {
    this.processCallback('modechange', e);
    if(this.tool.events.onModechange) {
      this.tool.events.onModechange(e);
    }
  }

  /**
   * 绘制可触发事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawActionableHandle(e) {
    this.processCallback('actionable', e);
    if(this.tool.events.onActionable) {
      this.tool.events.onActionable(e);
    }
  }

  /**
   * 绘制渲染事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawRenderHandle(e) {
    this.processCallback('render', e);
    if(this.tool.events.onRender) {
      this.tool.events.onRender(e);
    }
  }

  /**
   * 绘制组合合并事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawCombineHandle(e) {
    this.processCallback('combine', e);
    if(this.tool.events.onCombine) {
      this.tool.events.onCombine(e);
    }
  }

  /**
   * 绘制组合拆分事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawUncombineHandle(e) {
    this.processCallback('uncombine', e);
    if(this.tool.events.onUncombine) {
      this.tool.events.onUncombine(e);
    }
  }

  /**
   * 绘制拖拽停止事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawDragVertexHandle(e) {
    this.processCallback('dragVertex', e);
    if(this.tool.events.onDragVertex) {
      this.tool.events.onDragVertex(e);
    }
  }

  /**
   * 绘制组合事件
   * @param {*} e 事件参数
   * @ignore
   */
  drawUnionHandle(e) {
    this.processCallback('union', e);
    if(this.tool.events.onUnion) {
      this.tool.events.onUnion(e);
    }
  }

  /**
   * 绘制键盘按下事件
   * @param {*} e 事件参数
   * @ignore
   */
  keyDown(e) {
    this.processCallback('keydown', e);
    if(this.tool.events.onKeyDown) {
      this.tool.events.onKeyDown(e);
    }
  }

  /**
   * 触发事件
   * @param {String} type 事件类型
   * @param {*} e 事件参数
   * @ignore
   */
  processCallback(type, e) {
    // console.log("processCallback", type, e);
    this.tool.getEventBus().$emit('edittool.' + type + '.' + this.tool.event_suffix, e);
  }
}

export default Event;
